package com.person.webdemoproject2.controller;

import com.alibaba.fastjson2.JSON;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.person.webdemoproject2.pojo.*;
import com.person.webdemoproject2.service.UploadService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.awt.*;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
@CrossOrigin
@Slf4j
@RestController
@RequestMapping("/upload")
public class UploadController {
    @Autowired
    UploadService uploadService;
    @Autowired
    private com.person.webdemoproject2.utils.aliyunossUtils aliyunossUtils;



    @PostMapping("/photo")
    public Result uploadphoto( MultipartFile image, @RequestHeader String token) throws Exception {
        log.info("文件上传 {}",image.getOriginalFilename());



        if ( image != null &&!image.isEmpty()){

           String url = aliyunossUtils.upload(image);

            log.info("文件上传完成 {}",url);

            uploadService.uploadphoto(url,token);

            return Result.success(url);
        }

       return Result.error("发生错误,联系管理员");
    }
    @PostMapping("/post")
    public Result uploadpost(  @RequestParam List<MultipartFile> image,
                              @RequestParam Post post, @RequestHeader String token) throws IOException {
        List<String> images =new ArrayList<>();
        String video_url=null;
        if (image != null && !image.isEmpty()) {

            // 处理图片
            for (MultipartFile file : image) {
                String url = aliyunossUtils.upload(file);

                images.add(url);
            }
        }

        log.info("用户上传帖子内容 {} {} {} {}",images,video_url,token,post);

        uploadService.uploadpost(images,video_url,token,post);
        return Result.success("帖子发布成功");
    }
    @PostMapping("/video")
    public Result uploadVideo(
            @RequestParam(value = "video",required = false) MultipartFile video,
            @RequestParam(value = "image",required = false)  List<MultipartFile> image,
            // 接收文件
            @RequestParam(value = "post") String postJson,  // 接收 JSON 字符串
            @RequestHeader String token) throws Exception {

        // 将接收到的 JSON 字符串转换为 Post 对象
        ObjectMapper objectMapper = new ObjectMapper();
        Post post = objectMapper.readValue(postJson, Post.class);


        List<String> images = new ArrayList<>();
        String video_url= null;
        if (video != null&&!video.isEmpty()) {

//            video_url = null;
            video_url = aliyunossUtils.upload(video);
        }
        if (image != null && !image.isEmpty()) {

            // 处理图片
            for (MultipartFile file : image) {
                String url = aliyunossUtils.upload(file);

                images.add(url);
            }
        }

        log.info("用户上传帖子内容 {} {} {} {}",images,video_url,token,post);

        uploadService.uploadpost(images,video_url,token,post);
        return Result.success("帖子发布成功");

    }
    @PostMapping("/test")
    public Result uploadtest(
            @RequestParam List<MultipartFile> image,
            @RequestHeader String token) throws IOException {

        List<String> images = new ArrayList<>();
        String videoUrl = null;

        if (image != null && !image.isEmpty()) {
            // 处理图片上传
            for (MultipartFile file : image) {
                String url = aliyunossUtils.upload(file);
                log.info("上传成功的图片 URL: {}", url);
                images.add(url);
            }
        }



        return Result.success(images);  // 返回图片 URLs 和视频 URL
    }
    @PostMapping("/comment")
    public Result comment(@RequestBody Comment comment, @RequestHeader String token){
        log.info("用户上传评论内容 {} {}",comment.getComment(),comment.getPostid());

        if (comment.getComment()==null){
            return Result.error("内容为空");
        }
        uploadService.uploadComment(comment,token);
        return Result.success("评论成功");
    }
    @PostMapping("/recomment")
    public Result recomment(@RequestBody reComment recomment, @RequestHeader String token){
        log.info("用户上传评论其他评论内容 {} {}",recomment.getComment(),recomment.getComment_id());

        if (recomment.getComment()==null){
            return Result.error("内容为空");
        }
        uploadService.uploadreComment(recomment,token);
        return Result.success("评论成功");
    }
}
